<?
	/* ecomgear libs */
	function clean($content=NULL){ return htmlspecialchars(trim($content)); }
	function cleanhtml($content=NULL){ return htmlspecialchars(trim($content)); }
	function iso($str)
	{
		if(!isset($str) || is_null($str) ) return NULL;
		else return $str;
	}
	function encode($str){return str_replace("'", "$-$", str_replace("\"", "$--$", str_replace("<", "$|-$", str_replace(">", "$-|$", $str))));}
	function e($str){return str_replace("'", "$-$", str_replace("\"", "$--$", str_replace("<", "$|-$", str_replace(">", "$-|$", $str))));}
	function decode($str){return str_replace("$-$", "'", str_replace("$--$", "\"", str_replace("$|-$", "<", str_replace("$-|$", ">", $str))));}
	function d($str){return str_replace("$-$", "'", str_replace("$--$", "\"", str_replace("$|-$", "<", str_replace("$-|$", ">", $str))));}
	function escape($str)
	{ 
	  preg_match_all("/[\x80-\xff].|[\x01-\x7f]+/",$str,$r);
	  $ar = $r[0];
	  foreach($ar as $k=>$v) 
	  { 
		if(ord($v[0]) < 128)    
		  $ar[$k] = rawurlencode($v);    
		else    
		  $ar[$k] = "%u".bin2hex(iconv("UTF-8","UCS-2",$v));    
	  }    
	  return join("",$ar);
	}
	function dateadd($interval, $number, $date) { //add dates
		$darr = getdate($date); 
		$hours = $darr["hours"]; 
		$minutes = $darr["minutes"]; 
		$seconds = $darr["seconds"]; 
		$month = $darr["mon"]; 
		$day = $darr["mday"]; 
		$year = $darr["year"]; 
		switch ($interval) { 
		case "yyyy": $year +=$number; break; 
		case "q": $month +=($number*3); break; 
		case "m": $month +=$number; break; 
		case "y": 
		case "d": 
		case "w": $day +=$number; break; 
		case "ww": $day +=($number*7); break; 
		case "h": $hours +=$number; break; 
		case "n": $minutes +=$number; break; 
		case "s": $seconds +=$number; break; 
		} 
		$timestamp = mktime($hours ,$minutes, $seconds,$month ,$day, $year); 
		//echo "hehe";
		return $timestamp;
	}
	 function datediff($interval, $date1,$date2) { // get difference
		$diff = $date2 - $date1;
		switch ($interval) { 
		case "w": $retval = ceil($diff/604800); break; 
		case "d": $retval = ceil($diff/86400); break; 
		case "h": $retval = ceil($diff/3600); break; 
		case "n": $retval = ceil($diff/60); break; 
		case "s": $retval = $diff; break; 
		}
		return $retval;
	} 
	function str_cut($str, $cut, $lang='') 
	{
	   $returnstr=''; 
	   $i=0; 
	   $n=0; 
	   $str_length=strlen($str);
	   while (($n<$cut|| ($lang=='en'&&substr($str,$i,1)!=' ')) and ($i<=$str_length)) 
		 { 
		  $temp_str=substr($str,$i,1); 
		  $ascnum=Ord($temp_str);
		  if ($ascnum>=224)
		  {
			 $returnstr=$returnstr.substr($str,$i,3); 
			 $i=$i+3;
			 $n++; 
		  }
			elseif ($ascnum>=192)
		  { 
			 $returnstr=$returnstr.substr($str,$i,2);
			 $i=$i+2;
			 $n++; 
		  }
			elseif ($ascnum>=65 && $ascnum<=90) 
		  { 
			 $returnstr=$returnstr.substr($str,$i,1);
			 $i=$i+1;
			 $n++;
		  }
			else
		  { 
			 $returnstr=$returnstr.substr($str,$i,1); 
			 $i=$i+1;
			 $n=$n+0.5;
		  } 
		 } 
		 
		 if($lang=="en") {
			if ($str_length>$cut) $returnstr = $returnstr."...";
		}
		else {
			if ($str_length>$cut*3) $returnstr = $returnstr."...";
		}

		  return $returnstr; 
	}
function getip()
{
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  $ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  if($ip){
   array_unshift($ips, $ip); $ip = FALSE;
  }
  for($i = 0; $i < count($ips); $i++){
   if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])){
    $ip = $ips[$i];
    break;
   }
  }
}
return($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
function o2a($e){ 
	$e=(array)$e; 
	foreach($e as $k=>$v){ 
		if( gettype($v)=='resource' ) return; 
		if( gettype($v)=='object' || gettype($v)=='array' ) 
		$e[$k]=(array)o2a($v);
	}
	return $e;
}
function x2a($xml){
	$xmls = simplexml_load_string($xml);
	$json = json_encode($xmls);
	return $output = json_decode($json,TRUE);
}
function pre($arr){ echo "<pre>";print_r($arr);echo "</pre>"; }

function is_super_user($uid) {
	return ROOTUSERID==$uid;
}

function is_module_enabled($mid) {
 return $mid==1;
}

function is_enabled($field){ // 判断某个字段是否启用, 和语言配合使用
	global $option;
	if(isset($option[$field])) return $option[$field];
	else {
		$str=substr($field,-3);
		if(substr($str,0,1)=='_') { $field=substr($str,-2); if(isset($option[$field])) return $option[$field]; }
	}
	return 1;
}
// 快速排序, 适用于元素为数组的数组
function quick_sort($arr,$field='pid'){
	if(count($arr) <= 1) return $arr;
	$key = $arr[0];
	$left_arr = array();
	$right_arr = array();
	for ($i = 1; $i < count($arr); $i++){
		if ($arr[$i][$field] <= $key[$field])
			$left_arr[] = $arr[$i];
		else
			$right_arr[] = $arr[$i];
	}
	$left_arr = quick_sort($left_arr);
	$right_arr = quick_sort($right_arr);
	return array_merge($left_arr, array($key), $right_arr);
}
function seo_url($url=''){
	$arr=array(' ','\'','#',':','"','+','/','?','--','--','--');
	foreach($arr as $val){
		$url=str_replace($val,'-',$url);
	}
	return $url;
}
// 判断是否为手机
function is_mobile(){
 $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';  
 $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';     
 function CheckSubstrs($substrs,$text){  
  foreach($substrs as $substr)  
   if(false!==strpos($text,$substr)){  
    return true;  
   } 
   return false;  
 }
 $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAD');
 $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');  

 $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) ||  
     CheckSubstrs($mobile_token_list,$useragent);  

 if ($found_mobile){  
  return true;  
 }else{  
  return false;  
 }
}
// End of file common.php